package com.dense.kuiniu.floatcabinet.utils.timer;

import androidx.annotation.NonNull;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @date 2020/9/21
 */
public class CounterController {
    private int counter = 0;
    private long time;
    private ScheduledExecutorService executorService;
    private OnCounterListener mCounterListener;
    private ThreadFactory mThreadFactory;

    public CounterController(int time) {
        this.time = time;
        mThreadFactory = new ThreadFactory() {
            @Override
            public Thread newThread(@NonNull final Runnable r) {
                return new Thread() {
                    @Override
                    public void run() {
                        r.run();
                    }
                };
            }
        };
    }

    public OnCounterListener getCounterListener() {
        return mCounterListener;
    }

    public void setCounterListener(OnCounterListener counterListener) {
        mCounterListener = counterListener;
    }

    public void start() {
        startTimer();
        if(mCounterListener != null) {
            mCounterListener.onStart();
        }
    }

    public void pause() {
        if (executorService != null &&!executorService.isShutdown()) {
            executorService.shutdown();
        }
        if(mCounterListener != null) {
            mCounterListener.onPause();
        }
    }

    public void resume() {
        startTimer();
        if(mCounterListener != null) {
            mCounterListener.onResume();
        }
    }

    public void stop() {
        counter = 0;
        if (executorService != null &&!executorService.isShutdown()) {
            executorService.shutdown();
        }
        if(mCounterListener != null) {
            mCounterListener.onStop();
        }
    }

    public void complete() {
        counter = 0;
        if (executorService != null &&!executorService.isShutdown()) {
            executorService.shutdown();
        }
        if(mCounterListener != null) {
            mCounterListener.onComplete();
        }
    }


    private void startTimer() {
        if (executorService != null && !executorService.isShutdown()) {
            executorService.shutdown();
        }
        executorService = new ScheduledThreadPoolExecutor(1, mThreadFactory);
        executorService.scheduleAtFixedRate(mThreadFactory.newThread(new Runnable() {
            @Override
            public void run() {
                counter++;
                if (counter > time) {
                    complete();
                } else {
                    if(mCounterListener != null) {
                        mCounterListener.onCount(time, counter);
                    }
                }
            }
        }), 0, 1, TimeUnit.SECONDS);
    }

    public interface OnCounterListener extends OnCompleteListener {

        public void onStart();

        public void onCount(long time, int counter);

        public void onPause();

        public void onResume();

        public void onStop();


    }

    public interface OnCompleteListener {
        public void onComplete();
    }
} 